const Controllers = require('../controllers/Controller.js').controller;
const app = getApp();
Page({
  data: {
    globalStype:'',
    isFixedTop:false,
    tabbar:[],
    cloutGoodsPagedata:[],
    pageNumber:1,
    pageSize:10,
    isEnd:false,
    isloading:true,
    malllists:[
      {
        name:'云商城',
        id:''
      }
    ],
    shopId:'',
    mallindex:0,
  },

  onLoad(option) {
    this.setData({globalStype:app.globalData.globalStype})
    this.setData({
      tabbar: this.data.globalStype=='pinStyle'?app.globalData.tabbar:app.globalData.tabbar2,
    })
    if(app.globalData.shopId){
      this.setData({
        shopId:app.globalData.shopId,
      })
      this.selectLabel()
    }
  },
  tabChange(e){
    var index=e.detail.index;
    if(index==0){

      // wx.reLaunch({
      //   url: "/pages/Index/Index"
      // });

      wx.requestSubscribeMessage({
        tmplIds:['CgSkX1UN123rlfAQ9zXiMyfiF1ugDkOsd8yLmvcbHt0','IoewIlKNaUfOLOhKUOdPKMOnJxdtdqWGfOotuOB2R70','TA4G0mjgPxvzTWGRTqvB9rkUfdot7EvAwJpRYfcLKcc'],//'TA4G0mjgPxvzTWGRTqvB9rkUfdot7EvAwJpRYfcLKcc'
        success:res=>{
          console.log(res)
        },
        complete:()=>{
          wx.reLaunch({
            url: "/pages/Index/Index"
          });
        }
      })


    }
    if(index==2){
      wx.reLaunch({
        url: "/pages/cart/index/index"
      });
    }
    if(index==3){
      wx.reLaunch({
        url: "/pages/my/my-index/my-index"
      });
    }
  },
  onPageScroll: function (e) { // 获取滚动条当前位置
    console.log(e.scrollTop)
    let query = wx.createSelectorQuery()
    query.select('#malllists').boundingClientRect((rect) => {
      if(rect){
        let top = rect.top;
        if (top < e.scrollTop - 64) {
          this.setData({
            isFixedTop: true
          })
        } else {
          this.setData({
            isFixedTop: false
          })
        }
      }
    }).exec()
  },
  selectLabel(){
    Controllers.selectLabel({type:1,shopId:this.data.shopId}).then(res => {
      this.setData({
        malllists:this.data.malllists.concat(res.data)
      })
      this.cloutGoodsPage()
    });
  },
  cloutGoodsPage(labelId){
    if(this.data.labelId){
      Controllers.labelGoodsPage({
        pageSize:this.data.pageSize,
        pageNumber:this.data.pageNumber,
        shopId:this.data.shopId,
        labelId:this.data.labelId
      }).then(res => {
        if(res.data.list.length>0){
            for(let item of res.data.list){
              if(item.label){
                item.label=item.label.split('/')
              }
            }
          this.setData({
            cloutGoodsPagedata:this.data.cloutGoodsPagedata.concat(res.data.list),
  
          })
          
        }
        if(res.data.lastPage){
            this.setData({
              isEnd:true
            })
          }
        this.setData({
          isloading:false
        })
      }).catch(res=>{
        this.setData({isloading:false})
      })
    }else{
      Controllers.cloutGoodsPage({
        pageSize:this.data.pageSize,
        pageNumber:this.data.pageNumber,
        shopId:this.data.shopId
      }).then(res => {
        if(res.data.list.length>0){
            for(let item of res.data.list){
              if(item.label){
                item.label=item.label.split('/')
              }
            }
          this.setData({
            cloutGoodsPagedata:this.data.cloutGoodsPagedata.concat(res.data.list),
  
          })
          
        }
        if(res.data.lastPage){
            this.setData({
              isEnd:true
            })
          }
        this.setData({
          isloading:false
        })
      }).catch(res=>{
        this.setData({isloading:false})
      })
    }
    
  },
  
  onReachBottom() {
    if (this.data.isEnd) {
      return;
    } else {
      this.setData({
          pageNumber: this.data.pageNumber + 1,
      });
      this.cloutGoodsPage()
    }
  },
  changemall(e){
    this.setData({
      cloutGoodsPagedata:[],
      pageNumber:1,
      pageSize:10,
      isEnd:false,
      isloading:true,
      mallindex:e.currentTarget.dataset.index,
      shopId:this.data.shopId,
      labelId:e.currentTarget.dataset.id
    })
    this.cloutGoodsPage(e.currentTarget.dataset.id)
  },
  //获取通知权限
  getNotice(){
    wx.requestSubscribeMessage({
      tmplIds:['CgSkX1UN123rlfAQ9zXiMyfiF1ugDkOsd8yLmvcbHt0','IoewIlKNaUfOLOhKUOdPKMOnJxdtdqWGfOotuOB2R70','DlEG_b9R6f2bWjrW-jEmsoy_hcSHMMB9TPhh9q9GbCo'],
      success:res=>{
        console.log(res)
      },
      complete:()=>{
        wx.navigateTo({
          url:"/pages/Mall/Coupon"
        })
      }
    })
  }
})